import math

dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]

# 获取输入
nums = list(map(int, input().split(',')))
n = len(nums)
row = int(math.sqrt(n))
matrix = [nums[k:k+row] for k in range(0, n, row)] # 构造地图

queue = [] # 存放已感染的区域
for i in range(row):
    for j in range(row):
        if matrix[i][j] == 1:
            queue.append((i, j))

# 所有区域全部都被感染，或者没有被感染区域
if len(queue) == 0 or len(queue) == n:
    print(-1)
    exit(0)
    
healthy = n - len(queue)
day = 0
    
while queue and healthy > 0:
    new_queue = []
    # 遍历所有感染区域
    for q in queue:
        x, y = q
        for dir in dirs: # 朝4个方向遍历
            new_x = x + dir[0]
            new_y = y + dir[1]
            if new_x >= 0 and new_x < row and new_y >= 0 and new_y < row and matrix[new_x][new_y] == 0:
                matrix[new_x][new_y] = 1
                new_queue.append([new_x, new_y])
                healthy -= 1
    
    if new_queue: # 如果今天感染了新区域，day+1
        day += 1
    queue = new_queue    
    
print(day)
